package edu.chinasoft.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.User;
import edu.chinasoft.service.UserService;
import edu.chinasoft.service.UserServiceImp;

/**
 * 
 * @author cjogng
 * @date 2020年1月17日:下午2:02:23
 * @version V1.0
 * @Description: 更新servelt
*/
@WebServlet(name="myUpdateUserForServlet",value="/servlet/updateuserforservlet")
public class  UpdateUserForServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger( UpdateUserForServlet .class);
	private UserService userService = new UserServiceImp();
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" UpdateUserForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		//获取修改表单信息
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String user_password = request.getParameter("user_password");
		String user_desc = request.getParameter("user_desc");
		String user_flag = request.getParameter("user_flag");
		
		
		
		//封装到user，然后调用服务器去更新到数据库
		User user = new User(Integer.parseInt(user_id),user_name,user_password,user_desc,user_flag);
		boolean result = userService.modifyUserInfo(user);
		//如果更新成功，跳到查询用户页面
		
		
		//如果更新失败，显示失败页面
		if(result) {
			response.sendRedirect(""+request.getContextPath()+"/servlet/finduserpageforservlet");
		}else {
			response.sendRedirect(""+request.getContextPath()+"/updateusererror.jsp");
		}
		
		
		
		
		
		logger.debug(" UpdateUserForServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" UpdateUserForServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" UpdateUserForServlet >doPost end.......");
	}
	
}